package org.vacoor.ticket.handler.impl;

import java.io.UnsupportedEncodingException;

import org.vacoor.ticket.Constant;
import org.vacoor.ticket.bean.option.OrderState;
import org.vacoor.ticket.handler.ResultHandler;
import org.vacoor.ticket.util.ParseUtil;

public class OrderStateResultHandler implements ResultHandler<OrderState>{

	@Override
	/*
	{
	"tourFlag":"dc",  //标识,单程,往程,返程,现在只支持单程
	"waitTime":-1,	//等待时间,成功后为0/-1,其他情况也可能为-1,-2为没有足够票
	"waitCount":0,	//
	"orderId":"E260036893", //订单Id,只有成功后才会有该字段,如果没有检测,之前提交过,车次也会有,暂时没有更好的解决方法
	"requestId":5697595064290791684,
	"count":0
	"errorcode":"0",	//当没有足够票时候存在
	"msg":"没有足够的票!", //当密友足够票时存在
	}
	当waitTime 0/-1 且 orderId 时订购成功
		window.location.replace("/otsweb/order/confirmPassengerAction.do?method=payOrder&orderSequence_no=" + json.orderId);
	waitTime -2 铁道部说您占座失败 : " + json.msg + ', 赶紧重新来过!
	waitTime -3 订单被取消,铁道部无齿地撤销了您的订单, 赶紧重新下! 
	waitTime -4 ???正在处理中...
	waitTime <0 其他情况 未知的状态信息 : waitTime=' + json.waitTime + ', 可能已成功，请验证未支付订单. 
	waitTime >0 等待秒数,铁道部说的不准啊亲,
		waitCount 等待人数
	另外还可能直接跳转到查询页面
		
	//部分参考:https://dynamic.12306.cn/otsweb/js/order/orderQueue.js?version=5.42
	*/
	public OrderState handler(byte[] content) {
		String json;
		try {
			json = new String(content,Constant.ENCODING);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			json = new String(content);
		}
		String waitTime = ParseUtil.getValueFromJsonStr("waitTime", json);
		String orderId = ParseUtil.getValueFromJsonStr("orderId", json);
		String errorcode = ParseUtil.getValueFromJsonStr("errorcode", json);
		OrderState orderState = new OrderState();
		orderState.setMsg(json);
		orderState.setWaitTime(waitTime);
		if(!"".equals(errorcode.trim())) {
			orderState.setStateCode(OrderState.ERR_ENOUGH_TICKET);
		}
		if("0".equals(waitTime)||"-1".equals(waitTime) && !"".equals(orderId)) {
			orderState.setStateCode(OrderState.OK_SUCCESS);
			orderState.setOrderId(orderId);
			orderState.setMsg("订单貌似提交成功,订单号：" + orderId);
			return orderState;
		}
		if("-2".equals(waitTime)) {
			orderState.setStateCode(OrderState.ERR_FAILD);
			orderState.setMsg("占座失败");
			return orderState;
		}
		if("-3".equals(waitTime)) {
			orderState.setStateCode(OrderState.ERR_FAILD);
			orderState.setMsg("订单被取消");
			return orderState;
		}
		//如果小于3
		if("0".compareTo(waitTime) > 1) {
			orderState.setMsg("未知错误:" + json);
			return orderState;
		}
		orderState.setStateCode(OrderState.WAIT);
		orderState.setWaitTime(waitTime);
		return orderState;
		
		/*
		String orderId = ParseUtil.getValueFromJsonStr("orderId", json);
		String errorcode = ParseUtil.getValueFromJsonStr("errorcode", json); 
		
		OrderState orderState = new OrderState();
		orderState.setMsg(json);
		orderState.setWaitTime(ParseUtil.getValueFromJsonStr("waitTime", json));
		if(!"".equals(errorcode.trim())) {
			orderState.setStateCode(OrderState.NO_ENOUGH_TICKET);
		} else if(!"".equals(orderId)){
			orderState.setStateCode(OrderState.OK_SUCCESS);
			orderState.setOrderId(orderId);
			orderState.setMsg("订单貌似提交成功,订单号：" + orderId);
		}else {
			orderState.setStateCode(OrderState.WAIT);
		}
		return orderState;
		*/
	}
	public static void main(String[] args) {
		System.out.println("0".compareTo("0")>1);
	}
}
